package com.dabaozhan.common.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author houzhong
 * @date 2019-01-09 12:04
 */
public class Base64Utils {

    /**
     * 图片转成base64位，带前缀
     * @param img
     * @return
     */
    public static String base64(String img){
        String imgBase64 = "";

        try {
            File file = new File(img);
            byte[] content = new byte[(int) file.length()];
            FileInputStream finputstream = new FileInputStream(file);
            finputstream.read(content);
            finputstream.close();
           // imgBase64 += "data:image/jpeg;base64,";
            imgBase64 += new String(Base64.encodeBase64(content));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return imgBase64;
    }
}
